﻿
using System;
namespace NamedBinaryTag
{
	/// <summary>
	/// Helper to reverse integral types bytes order.
	/// </summary>
	public static class ByteReverser
	{
		/// <summary>
		/// Reverses a short's bytes.
		/// </summary>
		/// <param name="value">The value to transform.</param>
		/// <returns>The new value.</returns>
		public static short Reverse(short value)
		{
			byte[] bytes = BitConverter.GetBytes(value);
			Array.Reverse(bytes);
			return BitConverter.ToInt16(bytes, 0);
		}

		/// <summary>
		/// Reverses a int's bytes.
		/// </summary>
		/// <param name="value">The value to transform.</param>
		/// <returns>The new value.</returns>
		public static int Reverse(int value)
		{
			byte[] bytes = BitConverter.GetBytes(value);
			Array.Reverse(bytes);
			return BitConverter.ToInt32(bytes, 0);
		}

		/// <summary>
		/// Reverses a long's bytes.
		/// </summary>
		/// <param name="value">The value to transform.</param>
		/// <returns>The new value.</returns>
		public static long Reverse(long value)
		{
			byte[] bytes = BitConverter.GetBytes(value);
			Array.Reverse(bytes);
			return BitConverter.ToInt64(bytes, 0);
		}

		/// <summary>
		/// Reverses a float's bytes.
		/// </summary>
		/// <param name="value">The value to transform.</param>
		/// <returns>The new value.</returns>
		public static float Reverse(float value)
		{
			byte[] bytes = BitConverter.GetBytes(value);
			Array.Reverse(bytes);
			return BitConverter.ToSingle(bytes, 0);
		}

		/// <summary>
		/// Reverses a double's bytes.
		/// </summary>
		/// <param name="value">The value to transform.</param>
		/// <returns>The new value.</returns>
		public static double Reverse(double value)
		{
			byte[] bytes = BitConverter.GetBytes(value);
			Array.Reverse(bytes);
			return BitConverter.ToDouble(bytes, 0);
		}
	}
}
